package cn.zx.rmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * @Package: cn.zx.rmq.producer
 * @ClassName: OnewayProducer
 * @Author: Think
 * @CreateTime: 2021/6/28 11:11
 * @Description:同一个线程同步阻塞式地进行消息的发送，但是发送者不用等待broker的消息确认返回，即开始下一个消息的发送，比较快速，适合于对消息少量丢失不敏感的场景。
 */
@Slf4j
public class OnewayProducer {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("OnewayProducer-Group");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();

        for (int i = 0; i < 5; i++) {
            Message msg = new Message("KYC", "about-happy", ("客户拥有幸福点为：" + i*1000).getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.sendOneway(msg);
            log.info("当前消息发送完毕！");
        }

        producer.shutdown();
        log.info("所有消息发送完毕！");
    }

}
